/*
 * Copyright (C) 2008-2011 CBDP Consortium
 * 
 * All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.
 * 
 * This software consists of contributions made by many individuals, 
 * listed below:
 *
 * Author: 	Xabier Laiseca <xabier.laiseca@deusto.es>
 * 			David Ausín <david.ausin@deusto.es>
 */
package cbdp.server.contextreasoner.impl.jena;

import java.io.InputStream;

import com.hp.hpl.jena.util.Locator;
import com.hp.hpl.jena.util.TypedStream;

public class OSGiBundleLocator implements Locator
{
	private final static String BUNDLE_PROTOCOL_PREFIX = "bundle:";
	
	private final static String LOCATOR_NAME = "OSGiBundleLocator";
	
	
	@Override
	public String getName()
	{
		return LOCATOR_NAME;
	}

	@Override
	public TypedStream open(final String filename)
	{
		if (filename.startsWith(BUNDLE_PROTOCOL_PREFIX))
		{
			final String filenameWithoutProtocol = filename.substring(BUNDLE_PROTOCOL_PREFIX.length());
			
			final InputStream is = OSGiBundleLocator.class.getResourceAsStream(filenameWithoutProtocol);
			
			if (is == null)
			{
				return null;
			}
			
			return new TypedStream(is);
		}
		else return null;
	}

}
